package taskman;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TaskmanFileInputTest {

	String filename = "lol.txt";
	private String id;
	private String title;
	private String description;
	private double duration;
	private String deliverable;
	private String deadline;
	private String person;
	
	private void resetVars(){
		// resets all variables to default
		id="";
		title="";
		description="";
		duration=0;
		deliverable="";
		deadline="";
		person="";
	}
	
	@Test
	public void testTaskmanFileInput() {
		loadfromfile("bla.txt");
		 System.out.println("default file name");
	}


	@Test
	public void testTaskmanFileInputString() {
		loadfromfile(filename);
		 System.out.println("random file name");
	}
	

	private void loadfromfile(String filename) {
		// TODO Auto-generated method stub
		
		// reset variables
		resetVars();
		
		// create file reader object and read from file
		Scanner f=null;
		try {
			f=new Scanner(new FileInputStream(filename));
		} catch (FileNotFoundException e) {
			// if couldn't read from file, exit program
			System.out.println("Couldn't find the input file \""+filename+"\"! Terminating program.\n");
			System.exit(0);
		}

		// create String object to read the file line by line
		String readStr=null;
		// create String object to split the line into variable and value
		String[] readStrSplit;
		// var and val serve as pointers to readStrSplit
		String var=null, val=null;
		
		// loop until end of file is reached
		while (f.hasNext()){
			// read next line
			readStr=f.nextLine();
			
			// split line into variable and value
			readStrSplit=readStr.split("=", 2);
			var=readStrSplit[0];
			
			// before setting val, make sure readStrSplit has at least 2 elements in it
			if(readStrSplit.length>1)
				val=readStrSplit[1];
			else
				val="";
			
			
			
			// check what variable is and determine action
			if(var.equalsIgnoreCase("id")){
				// marks beginning of new task item
				// perform actions based on current variables values
				export();				
				
				// reset vars to default
				resetVars();
			
				// set id
				id=val;
			}
			// cases below are to set the variables
			else if(var.equalsIgnoreCase("title")){
				title=val;
			}else if(var.equalsIgnoreCase("description")){
				description=val;
			}else if(var.equalsIgnoreCase("duration")){
				// need to convert from String to double
				duration=Double.valueOf(val).doubleValue();
			}else if(var.equalsIgnoreCase("deliverable")){
				deliverable=val;
			}else if(var.equalsIgnoreCase("deadline")){
				deadline=val;
			}else if(var.equalsIgnoreCase("person")){
				person=val;
			}
			
			
		} // end of while loop
		
		// file reading ended. export data
		export();
		
		// close file reader
		f.close();
	}
	
	@Test
	public void export() {
		// if no id is entered, ignore this function
		if(id=="") return;
		
		/*
		 * insert code here for output stuff.
		 * variables are:
		 * 	private String id;
			private String title;
			private String description;
			private double duration;
			private String deliverable;
			private String deadline;
			private String person;
		 *
		 * assume all values are present
		 * 
		 */
		
		// the following is for debugging
		System.out.println("id="+id);
		System.out.println("title="+title);
		System.out.println("description="+description);
		System.out.println("duration="+duration);
		System.out.println("deliverable="+deliverable);
		System.out.println("deadline="+deadline);
		System.out.println("person="+person);
		System.out.println("****************************");
	}
}
